package com.minarto.tooltip {
	import de.polygonal.core.ObjectPool;
	import com.minarto.utils.GPool;
	import flash.utils.getDefinitionByName;
	import flash.events.*;
	import flash.display.*;


	/**
	 * @author KIMMINHWAN
	 */
	public class ToolTipManager {
		static private var _instance:ToolTipManager;
		static public function getInstance():ToolTipManager {
			return	_instance;
		}
		
		
		static public function init($stage:Stage):ToolTipManager {
			_instance = new ToolTipManager($stage);
			return	_instance;
		}
		
		
		private var _toolTip:DisplayObject,
					_toolTips:Vector.<DisplayObject> = new <DisplayObject>[];
		
		
		protected var	stage:Stage,
						canvas:Sprite,
						toolTipRender:Class;
					
					
		public function getToolTip():DisplayObject {
			return	_toolTip;
		}
		
		
		public function getToolTips():Vector.<DisplayObject> {
			return	_toolTips;
		}
		
		
		public function ToolTipManager($stage:Stage) {
			if (_instance) {
				throw new Error("don't create instance, use ToolTipManager.getInstance()");
			}
			
			stage = $stage;
			
			canvas = new Sprite;
			canvas.visible = false;
			canvas.tabChildren = false;
			canvas.tabEnabled = false;
			canvas.mouseChildren = false;
			canvas.mouseEnabled = false;
			$stage.addChild(canvas);
			
			$stage.addEventListener(ToolTipConst.TOOLTIP_REGISTER, onEvtRegister, false, 0, true);
			$stage.addEventListener(ToolTipConst.TOOLTIP_UNREGISTER, onEvtUnregister, false, 0, true);
		}
		
		
		protected function onEvtRegister($e:Event):void {
			var u:DisplayObject = $e.target as DisplayObject;
			if(u && u["toolTip"]){
				u.addEventListener(MouseEvent.ROLL_OVER, onRollOver, false, 0, true);
				u.addEventListener(MouseEvent.ROLL_OUT, onRollOut, false, 0, true);
			}
		}
		
		
		protected function onEvtUnregister($e:Event):void {
			var u:EventDispatcher = $e.target as EventDispatcher;
			if(u){
				u.removeEventListener(MouseEvent.ROLL_OVER, onRollOver);
				u.removeEventListener(MouseEvent.ROLL_OUT, onRollOut);
			}
		}
		
		
		protected function onRollOver($e:MouseEvent):void {
			var tdata:* = $e.target.toolTip;
			if(!tdata)	return;
			
			if(!_toolTip){
				toolTipRender = getDefinitionByName("ToolTip") as Class;
				_toolTip = new toolTipRender as DisplayObject;
				_toolTips[0] = _toolTip;
			}
			
			_toolTip["data"] = tdata;
			canvas.addChild(_toolTip);
			_toolTips.length = 1;
			
			var compares:Array = tdata["compares"];
			if(compares && compares.length){
				var p:ObjectPool = GPool.getPool(toolTipRender);
				var i:uint;
				var c:uint = compares.length;
				for(i = 0; i < c; i++){
					var t:DisplayObject = p.object;
					t["data"] = tdata;
					canvas.addChild(t);
					_toolTips.push(t);
				}
			}
			
			canvas.visible = true;
		}
		
		
		protected function onRollOut($e:MouseEvent):void {
			canvas.visible = false;
			
			var i:uint = _toolTips.length;
			if(i > 1){
				var p:ObjectPool = GPool.getPool(toolTipRender);
			}
			while( i --){
				var t:DisplayObject = _toolTips[i];
				canvas.removeChild(t);
				if(i > 0){
					p.object = t;
				}
			}
		}
	}
}